package app.services.impl;

import java.util.List;

import app.models.Log;
import app.repositories.LogRepository;
import app.repositories.Repository;
import app.services.LogService;
import app.session.UserSession;
import app.util.Credentials;
import br.com.caelum.vraptor.ioc.Component;

@Component
public class LogServiceImpl extends GenericServiceImpl<Log, Long> implements LogService {

	private final LogRepository repository;
	private final UserSession userSession;
	
	public LogServiceImpl (LogRepository repository, UserSession userSession){
		this.repository = repository;
		this.userSession = userSession;
		super.setGenericRepository((Repository<Log, Long>) repository);
	}
	
	public void createLog(String descricao){
		Log log = new Log();
		
		log.setDescricao(descricao);
		
		if(userSession != null && userSession.getUser() != null){
			log.setIdUsuario(userSession.getUser().getId().intValue());
			log.setUsuario(userSession.getUser());
		} else {
			if(Credentials.idUsuario != null){
				log.setIdUsuario(Credentials.idUsuario);
			} else {
				log.setIdUsuario(0);
			}
		}
		
		create(log);
	}
	
	public List<Log> findLogsByPeriodAndUser (String dataInicial, String dataFinal, String usuario) {
		return repository.findLogsByPeriodAndUser(dataInicial, dataFinal, usuario);
	}

}
